<template>
  <el-form :inline="true" :model="searchForm" class="demo-form-inline">
    <el-form-item v-for="item in columns" v-show="!item.hide" :key="item.name" :prop="item.name" :label="item.label">
      <el-input v-if="item.type === 'input'" v-model="searchForm[item.name]" @change="formValueChange"></el-input>

      <el-input-number
        v-else-if="item.type === 'number'"
        v-model="searchForm[item.name]"
        :precision="item.options.precision"
        :step="item.options.step"
        :min="item.options.min"
        :max="item.options.max"
        @change="formValueChange"
      ></el-input-number>

      <el-input v-else-if="item.type === 'textarea'" type="textarea" v-model="searchForm[item.name]" @change="formValueChange"></el-input>

      <el-select v-else-if="item.type === 'select'" style="width: 100%" v-model="searchForm[item.name]" :placeholder="item.placeholder" @change="formValueChange">
        <el-option v-for="opt in item.dataSource" :key="opt.value" :label="opt.label" :value="opt.value"></el-option>
      </el-select>

      <el-cascader
        v-else-if="item.type === 'cascader'"
        style="width: 100%"
        v-model="searchForm[item.name]"
        :placeholder="item.placeholder"
        :options="item.dataSource"
        @change="formValueChange"
      ></el-cascader>

      <el-switch v-else-if="item.type === 'switch'" v-model="searchForm[item.name]" @change="formValueChange"></el-switch>

      <el-checkbox-group v-else-if="item.type === 'checkbox'" v-model="searchForm[item.name]" @change="formValueChange">
        <el-checkbox v-for="opt in item.dataSource" :key="opt.value" :label="opt.value" :name="item.name">{{ opt.label }}</el-checkbox>
      </el-checkbox-group>

      <el-radio-group v-else-if="item.type === 'radio'" v-model="searchForm[item.name]" @change="formValueChange">
        <el-radio v-for="opt in item.dataSource" :key="opt.value" :label="opt.label" :value="opt.value"></el-radio>
      </el-radio-group>

      <el-date-picker
        v-else-if="item.type === 'date'"
        type="date"
        value-format="yyyy-MM-dd"
        style="width: 100%"
        :placeholder="item.placeholder"
        v-model="searchForm[item.name]"
        @change="formValueChange"
      ></el-date-picker>

      <el-date-picker
        v-else-if="item.type === 'datetime'"
        type="datetime"
        value-format="yyyy-MM-dd HH:mm:ss"
        style="width: 100%"
        :placeholder="item.placeholder"
        v-model="searchForm[item.name]"
        @change="formValueChange"
      ></el-date-picker>

      <el-time-picker
        v-else-if="item.type === 'time'"
        value-format="HH:mm:ss"
        style="width: 100%"
        :placeholder="item.placeholder"
        v-model="searchForm[item.name]"
        @change="formValueChange"
      ></el-time-picker>
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="onSubmit">查询</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
export default {
  data() {
    return {
      searchForm: {}
    }
  },
  props: {
    columns: {
      type: Array
    }
  },
  methods: {
    onSubmit() {
      this.$emit('search', this.searchForm)
      console.log('search!')
    },
    formValueChange() {
      this.$emit('change', this.searchForm)
      console.log('search!')
    }
  }
}
</script>
